from Subject import Subject
import pygame;
import sys;
from time import time;
from pygame import QUIT
from pygame import KEYDOWN,K_ESCAPE,K_RETURN, MOUSEBUTTONUP

class Model(Subject):

    def __init__(self, scene):
        super(Model, self).__init__()
        self.scene = scene;
        self.nextSceneTime = time()+self.scene.time;
    
    @property
    def text(self):
        return self.scene.text
    
    @property
    def isLongText(self):
        return self.scene.isLongText
    
    @property
    def image(self):
        return self.scene.image
    
    @property
    def position(self):
        return self.scene.position
    
    @property
    def background(self):
        return self.scene.background
    
    def keyEscPress(self):
        self.scene.END = True;
    
    def keyNextPress(self):
        self.scene.advance()
        if not self.scene.END:
            self.nextSceneTime = time()+self.scene.time
        
    
    def run(self):
        while(not self.scene.END):
            if time() >= self.nextSceneTime:
                self.keyNextPress();
                if not self.scene.END:
                    self.notify();
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                elif event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        self.keyEscPress()
                    if event.key == K_RETURN:
                        self.keyNextPress()
                elif event.type == MOUSEBUTTONUP:
                    self.keyNextPress()
                if not self.scene.END:
                    self.notify()